package ru.zombator.taskman.client.ui.dialogs;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.layout.GridData;

import ru.zombator.taskman.client.ui.images.ImageNames;
import ru.zombator.taskman.client.ui.images.Registry;

/**
 * Диалог создания нового проекта
 * @author nvamelichev
 *
 */
public class CreateProjectDialog extends TitleAreaDialog {
	private Text projectName;
	private Text projectPassword;
	
	private String name = "";
	private String password = "";
	
	private boolean isValid = true;
	private String errorMessage;
	
	private static final String DEFAULT_MESSAGE = "Введите информацию о проекте";
	
	private final ModifyListener VALIDATION_LISTENER = new ModifyListener() {
		@Override
		public void modifyText(ModifyEvent e) {
			validateDialog();
			name = projectName.getText();
			password = projectPassword.getText();
		}
	};
	
	private void validateDialog() {
		boolean oldIsValid = isValid;
		isValid = (projectName.getText().length() > 0 &&
				projectPassword.getText().length() > 0);
		String newErrorMessage = "";
		if (!isValid) {
			if (projectName.getText().length() == 0)
				newErrorMessage = "Введите имя проекта";
			else if (projectPassword.getText().length() == 0)
				newErrorMessage = "Введите пароль проекта";
			
			if (oldIsValid != isValid || !newErrorMessage.equals(errorMessage)) {
				setMessage(newErrorMessage, IMessageProvider.ERROR);
			}
		} else {
			setMessage(DEFAULT_MESSAGE);
		}
		oldIsValid = isValid;
		errorMessage = newErrorMessage;
		
		Button okButton = getButton(IDialogConstants.OK_ID);
		if (okButton != null)
			okButton.setEnabled(isValid);
	}
	
	public String getName() {
		return this.name;
	}
	
	public String getPassword() {
		return this.password;		
	}
	
	public CreateProjectDialog(Shell parentShell) {
		super(parentShell);
	}
	
	@Override
	public void create() {
		super.create();
		
		setTitle("Создать проект");
		setMessage(DEFAULT_MESSAGE);
		getShell().setText("Создать проект");
		
		validateDialog();
	}
	
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite z = (Composite) super.createDialogArea(parent);
		z.setLayout(new GridLayout(1, false));
		
		Composite area = new Composite(z, SWT.NONE);
		area.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		GridLayout gl_area = new GridLayout(2, false);
		area.setLayout(gl_area);
		
		Label label = new Label(area, SWT.NONE);
		label.setText("Проект:");
		
		projectName = new Text(area, SWT.BORDER);
		projectName.setTextLimit(50);
		projectName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		projectName.addModifyListener(VALIDATION_LISTENER);
		
		Label label_1 = new Label(area, SWT.NONE);
		label_1.setText("Пароль:");
		
		projectPassword = new Text(area, SWT.BORDER | SWT.PASSWORD);
		projectPassword.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		projectPassword.addModifyListener(VALIDATION_LISTENER);
		
		setTitleImage(Registry.getImage(ImageNames.CREATE_PROJECT));
		
		return area;
	}
}
